package com.vivo.tasks;


import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.vivo.Utils.SendDingDingMsgUtil;
import com.vivo.dingtalk.entity.DingUserInfo;
import com.vivo.dingtalk.service.DingUserInfoService;
import com.vivo.dingtalk.web.vm.LinkMsgVM;
import com.vivo.dingtalk.web.vm.ResultVM;
import com.vivo.system.entity.VivoDingConf;
import com.vivo.system.service.VivoDingConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TaskDaohuo {

    @Autowired
    private SystemService systemService;

    @Autowired
    private DingUserInfoService dingUserInfoService;

    @Autowired
    private VivoDingConfService vivoDingConfService;

    /**
     * 发送需要进行到货通知的人员信息
     */
    public void SyncData() {

        List<String> userList = new ArrayList<>();
        userList.add("15951704592"); // 杨小侠
        userList.add("15251863989"); // 陶利燕

        for (String phone: userList) {
            LinkMsgVM linkMsg = new LinkMsgVM();
            linkMsg.setContent("工厂到货通知");
            linkMsg.setPhone(phone);
            linkMsg.setTitle("百胜IDC");
            linkMsg.setUrl("http://vivo.jsbstx.com/a/emp/report/daohuo?comp=1");
            User user = null;
            if (!StringUtils.isBlank(linkMsg.getPhone())) {
                user = systemService.getUserByLoginName(phone);
            }

            if (user == null) {
                System.out.println("找不到对应的用户信息！");
                continue;
            }

            DingUserInfo dingUserInfo = dingUserInfoService.getByMobile(user.getLoginName());
            if (dingUserInfo == null) {
                System.out.println("找不到对应的用户钉钉登陆信息！请提醒其进行钉钉登陆操作！");
                continue;
            }
            linkMsg.setDingUserId(dingUserInfo.getDingUserId());

            VivoDingConf dingConf = vivoDingConfService.getByComp(user.getCompany().getId());
            if (dingConf == null) {
                System.out.println("该二级区域还没有进行钉钉绑定设置，提醒管理员进行相关绑定操作！二级区域："
                        + user.getCompany().getName());
                continue;
            }
            linkMsg.setAccessToken(dingConf.getAccessToken());
            linkMsg.setAgentId(Long.parseLong(dingConf.getAgentId()));

            OapiMessageCorpconversationAsyncsendV2Response rsp = null;
            try {
                rsp = SendDingDingMsgUtil.sendWorkMsg(linkMsg);
                System.out.println("钉钉链接消息发送成功！");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("消息发送失败" + e.getMessage());
            }
        }

        System.out.println("通知发送完毕！");
    }
}
